//引入核心库
import Vue from 'vue'
//引入vuex的核心库
import Vuex from 'vuex'
//调用中间件
Vue.use(Vuex)


//导出封装好的仓库
export default new Vuex.Store({
    state: {
        userinfo: sessionStorage.getItem('userinfo') ? JSON.parse(sessionStorage.getItem('userinfo')) : null
    },
    mutations: {
        CHANGE_USER(state, payload) {
            if (payload) {
                state.userinfo = payload
                // 把payload存储到会话存储中
                sessionStorage.setItem('userinfo', JSON.stringify(payload))
            } else {
                sessionStorage.removeItem('userinfo')
            }
        }
    },
    actions: {
        changeUserAction({ commit }, payload) {
            commit('CHANGE_USER', payload)
        }
    },
    getters: {
        getUserInfo(state) {
            return state.userinfo;
        }
    },
    modules: {}
})